package com.jack.vipmis.vip.api.web.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jack.vipmis.common.web.controller.BaseController;
import com.jack.vipmis.vip.api.util.MsgInfo;
import com.jack.vipmis.vip.wei.entity.WeiEntity;
import com.jack.vipmis.vip.wei.service.WeiEntityService;

/**
 * 
 * @author JackLiang
 * @version 1.1
 * @date 2014-8-27
 */
@Controller
@RequestMapping(value = "api/wei")
public class WeiApiController extends BaseController<WeiEntity, Long> {

	@Autowired
	private WeiEntityService	weiEntityService;

	/**
	 * 添加
	 * 
	 * @param w
	 * @return
	 */
	@RequestMapping(value = "add", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addWei(@RequestParam("title") String title, @RequestParam("userId") long userId,
			@RequestParam("content") String content, @RequestParam("images") String images) {
		try {
			WeiEntity m = new WeiEntity();
			m.setTitle(title);
			m.setContent(content);
			m.setUserId(userId);
			m.setImages(images);
			WeiEntity w = weiEntityService.save(m);
			if (w != null) {
				return MsgInfo.ok(w);
			} else {
				return MsgInfo.fail();
			}
		} catch (Exception e) {
			e.printStackTrace();
			return MsgInfo.fail();
		}

	}

	/**
	 * 微贴列表分页查询
	 * 
	 * @param page
	 * @param text
	 * @return
	 */
	@RequestMapping(value = "list", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> list(@RequestParam("page") int page, @RequestParam("text") String text) {
		try {
			Page<WeiEntity> result = weiEntityService.list(page, text);
			return MsgInfo.ok(result.getContent());
		} catch (Exception e) {
			return MsgInfo.fail();
		}
	}
}
